package lab6;

public class MergeIntArrays {

	public static int[] mergeUnsorted(int[] a, int[]b)
	{
		MergeSort.mergeSort(a);
		MergeSort.mergeSort(b);
		return mergeSorted(a,b);
	}
	public static int[] mergeSorted(int[] a, int[] b)
	{
		int aPos=0, bPos=0;
		int[] merged = new int[a.length+b.length];
		for (int i = 0; i < merged.length; i++)
		{
			if (aPos>=a.length)
			{
				merged[i]=b[bPos];
				bPos++;
			}
			else if (bPos>=b.length)
			{
				merged[i]=a[aPos];
				aPos++;
			}
			else if (a[aPos]<=b[bPos])
			{
				merged[i]=a[aPos];
				aPos++;
			}
			else
			{
				merged[i]=b[bPos];
				bPos++;
			}
		}
		return merged;
	}
	
	
	public static void main(String[] args) {
		int[] a = {10, -4, 5, 17, 8, 10, 15};
		int[] b = {-1, 4, -9, 21, 14, 16};
		int[] merged = mergeUnsorted(a,b);
		for (int i = 0; i<merged.length; i++) System.out.print(merged[i]+" ");
	}

}
